<?php
class Util
{
	/**
     * 操作提示
     *
     * @param String $msg
     * @param string $jumpUrl
     * @param int $time
     */
    public static function msgRedirect($msg,$jumpUrl='-1',$time=2)
    {
        $tpl = self::getTpl();
        $tpl->assign('msg',$msg);
        $tpl->assign('jumpUrl',$jumpUrl);
        $tpl->assign('time',$time);
        $tpl->display('system/msgRedirect.tpl');
        exit();
    }
    
    public static function msgReturnJson($msg, $status=0)
    {
    	$row['msg'] = $msg;
    	$row['status'] = $status;
    	$json = json_encode($row);
    	exit($json);
    }
    
    public static function ajaxReturn($msg)
    {
    	
    }
	
	public static function getIp()
	{
		if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && preg_match_all('/(\d{1,3}\.){3}\d{1,3}/s', $_SERVER['HTTP_X_FORWARDED_FOR'], $mat)) {
            foreach ( $mat[0] as $ip ) {
                if (!preg_match("/^(?:10|172\.16|192\.168)\./", $ip)) {
                    return $ip;
                }
            }
            return $ip;
        }
        elseif (isset($_SERVER["HTTP_X_REAL_IP"]) && preg_match_all('/(\d{1,3}\.){3}\d{1,3}/s', $_SERVER['HTTP_X_REAL_IP'], $mat)) {
            foreach ( $mat[0] as $ip ) {
                if (!preg_match("/^(?:10|172\.16|192\.168)\./", $ip)) {
                    return $ip;
                }
            }
            return $ip;
        }
        elseif (isset($_SERVER["HTTP_FROM"]) && preg_match('/(?:\d{1,3}\.){3}\d{1,3}/', $_SERVER["HTTP_FROM"])) {
            return $_SERVER["HTTP_FROM"];
        }
        else
            return $_SERVER['REMOTE_ADDR'];
	}
	
	/**
     * 获得一个smarty实例
     *
     * @param array $params
     * @return obj
     */
    public static function getTpl($params=array())
    {
        require_once(ROOT_DIR.'include/Template.php');
        return new Template();
    }
	
}